<?php

class ClassController extends BaseController {

    public function __construct($action, $urlValues) {
        if (!Helper::isLogin()) {
            $_SESSION['lastUrl'] = Helper::currentPageURL();
            header('Location: ' . BASE_URL . 'account/login');
        }
        parent::__construct($action, $urlValues);
        require("models/class.php");
        $this->model = new ClassModel();
    }

    public function index() {
        $this->view->output($this->model->index());
    }

    protected function create() {
        if (!empty($_POST)) {

            if (!isset($_POST['keyword']) || $_POST['keyword'] == "")
                $_POST['keyword'] = $_POST['classname'];
            $pr = array();
            $pr['DayOfWeek'] = $_POST['sltDayOfWeek'];

            $_POST['params'] = json_encode($pr);

            //$ts = get_object_vars(json_decode($_POST['params']));
            unset($_POST['sltDayOfWeek']);
            $result = $this->model->create($_POST);

            if ($result) {
                $alert = array(
                    'class' => 'alert-success',
                    'message' => 'Add class successful'
                );
                $_SESSION['message'] = $alert;
                header('Location: ' . BASE_URL . 'class/index');
            }
        }
        $this->view->output($this->model->create(''));
    }

    //update
    protected function update() {
        //load data update post from client
        if (!empty($_POST)) {
            //get data from POST
            $id = $_POST['classes_id'];
            foreach ($_POST as $col => $value) {
                $data[$col] = $value;
            }
            $pr['DayOfWeek'] = $_POST['sltDayOfWeek'];

            $data['params'] = json_encode($pr);

            //$ts = get_object_vars(json_decode($_POST['params']));
            unset($data['sltDayOfWeek']);

            unset($data['classes_id']);
            //update item -> return total rows affected
            $result = $this->model->edit($id, $data);
            echo $result;
            if ($result) {
                $alert = array(
                    'class' => 'alet-success',
                    'message' => 'U class successful'
                );
                $_SESSION['message'] = $alert;
                header('Location: ' . BASE_URL . 'class/index');
            }
        }
        //load form update
        $this->view->output($this->model->edit($id = $this->urlValues['id'], $data = ''));
    }

    //detele action
    protected function delete() {
        if (!empty($_POST)) {
            $result = $this->model->delete('classes', 'classes_id IN(' . implode(',', $_POST['id']) . ')');
        } else {
            $result = $this->model->delete('classes', 'classes_id=' . $this->urlValues['id']);
        }
        if ($result) {
            $alert = array(
                'class' => 'alet-success',
                'message' => 'Delete class successful'
            );
            $_SESSION['message'] = $alert;
        }
        header('Location: ' . BASE_URL . 'class/index');
    }

    public function getDayOfWeek() {
        $this->view->output($this->model->getDayOfWeek($id = $this->urlValues['id'], $data = ''),"empty");
    }

}
